package controller.login;

import controller.BaseServlet;
import dao.UserDao;
import model.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@WebServlet("/login/*")
public class LoginServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    public void toLogin(HttpServletRequest req, HttpServletResponse resp){
        try {
            Map<String, String> params = getParams(req);
            String username =params.get("form-username");
            String password =params.get("form-password");
            String checkCode =params.get("checkCode");
            String remember = params.get("remember");
            if(username == null || password == null || checkCode == null){
                req.getRequestDispatcher("/view/login").forward(req,resp);
            }
            User user = userService.findByName(username);
            if(user == null){
                req.getRequestDispatcher("/view/login").forward(req,resp);
            }else{
                if(!user.getPassword().equals(password) ||
                        !checkCode.equals(req.getSession().getAttribute("sessionCode"))){
                    req.getRequestDispatcher("/view/login").forward(req,resp);
                }else{
                    //记住我
                    if("1".equals(remember)){
                        Cookie cookie = new Cookie("loginUser", user.getId() + "");
                        cookie.setPath("/");
                        cookie.setMaxAge(60 * 60 *24 * 7);
                        resp.addCookie(cookie);
                    }
                    user.setLoginTime(new Date());
                    userService.update(user);
                    req.getSession().setAttribute("loginUser",user);
                    req.getRequestDispatcher("/view/home").forward(req,resp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void toRegister(HttpServletRequest req, HttpServletResponse resp){
        try {
            Map<String, String> params = getParams(req);
            if(!params.get("form-password").equals(params.get("form-password2"))){
                resp.sendRedirect(req.getContextPath() + "/view/login");
            }else{
                User user = userService.findByName(params.get("form-username"));
                if(user != null){
                    req.getRequestDispatcher("/view/login").forward(req,resp);
                }else{
                    User user1 = new User();
                    user1.setEmail(params.get("form-email"));
                    user1.setUsername(params.get("form-username"));
                    user1.setPassword(params.get("form-password"));
                    user1.setRegisterTime(new Date());
                    int result = userService.save(user1);
                    req.getRequestDispatcher("/view/login").forward(req,resp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
